package cn.iocoder.yudao.module.call.controller.admin.ivrknowledgeissue;

import cn.hutool.json.JSONObject;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import cn.iocoder.yudao.module.call.dal.dataobject.ivrknowledgeissuevoice.IvrKnowledgeIssueVoiceDO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.call.controller.admin.ivrknowledgeissue.vo.*;
import cn.iocoder.yudao.module.call.dal.dataobject.ivrknowledgeissue.IvrKnowledgeIssueDO;
import cn.iocoder.yudao.module.call.service.ivrknowledgeissue.IvrKnowledgeIssueService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - ivr-知识库问题")
@RestController
@RequestMapping("/call/ivr-knowledge-issue")
@Validated
public class IvrKnowledgeIssueController {

    @Resource
    private IvrKnowledgeIssueService ivrKnowledgeIssueService;

    @GetMapping("/delKnowledgeIssueVoiceById")
    @Operation(summary = "批量删除知识库回答语音列表")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<String> delKnowledgeIssueVoiceById(@RequestParam( "id") Long flowId) {
        ivrKnowledgeIssueService.delKnowledgeIssueVoiceById(flowId);
        return success("删除成功");
    }
    @GetMapping("/delKnowledgeIssueVoiceByFlowId")
    @Operation(summary = "批量删除知识库回答语音列表")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<String> delKnowledgeIssueVoiceByFlowId(@RequestParam( "flowId") Long flowId) {
        ivrKnowledgeIssueService.delKnowledgeIssueVoiceByFlowId(flowId);
        return success("删除成功");
    }
    @PostMapping("/knowledgeIssueVoiceByIdCreate")
    @Operation(summary = "知识库批量生成回答语音")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<String> knowledgeIssueVoiceByIdCreate(@RequestBody IvrKnowledgeIssueVoiceDO createReqVO) {
        ivrKnowledgeIssueService.knowledgeIssueVoiceByIdCreate(createReqVO);
        return success("创建成功");
    }

    @PostMapping("/knowledgeIssueVoiceByIdCreateManual")
    @Operation(summary = "知识库根据Id手动长传回答语音")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<String> knowledgeIssueVoiceByIdCreateManual(@RequestParam("id") Long id,
                                                                    @RequestParam("file") MultipartFile file) {
        IvrKnowledgeIssueVoiceDO createReqVO = new IvrKnowledgeIssueVoiceDO();
        createReqVO.setId(id);
        createReqVO.setFile(file);
        ivrKnowledgeIssueService.knowledgeIssueVoiceByIdCreateManual(createReqVO);
        return success("操作成功");
    }


    @PostMapping("/knowledgeIssueVoiceListCreate")
    @Operation(summary = "知识库批量生成回答语音")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<String> knowledgeIssueVoiceListCreate(@RequestBody IvrKnowledgeIssueVoiceDO createReqVO) {
        ivrKnowledgeIssueService.knowledgeIssueVoiceListCreate(createReqVO);
        return success("创建成功");
    }
    @GetMapping("/KnowledgeIssueByFlowId")
    @Operation(summary = "知识库回答列表")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<List<JSONObject>> KnowledgeIssueByFlow(@RequestParam( "flowId") Long flowId) {
        List<JSONObject> jsonObjects = ivrKnowledgeIssueService.KnowledgeIssueByFlowId(flowId);
        return success(jsonObjects);
    }

    @PostMapping("/createLIst")
    @Operation(summary = "创建ivr-知识库问题")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<Long> createLIst(@Valid @RequestBody List<IvrKnowledgeIssueSaveReqListVO> createReqVO,@RequestParam( "flowId") Long flowId,@RequestParam( "flowId") String voiceType) {
        return success(ivrKnowledgeIssueService.createIvrKnowledgeIssueList(createReqVO, flowId,voiceType));
    }
    @PostMapping("/create")
    @Operation(summary = "创建ivr-知识库问题")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<Long> createIvrKnowledgeIssue(@Valid @RequestBody IvrKnowledgeIssueSaveReqVO createReqVO) {
        return success(ivrKnowledgeIssueService.createIvrKnowledgeIssue(createReqVO));
    }

    @PostMapping("/updateType")
    @Operation(summary = "创建ivr-知识库问题")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<Boolean> updateType(@Valid @RequestBody IvrKnowledgeIssueSaveReqVO createReqVO) {
        ivrKnowledgeIssueService.updateIvrKnowledgeIssueType(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新ivr-知识库问题")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:update')")
    public CommonResult<Boolean> updateIvrKnowledgeIssue(@Valid @RequestBody IvrKnowledgeIssueSaveReqVO updateReqVO) {
        ivrKnowledgeIssueService.updateIvrKnowledgeIssue(updateReqVO);
        return success(true);
    }


    @DeleteMapping("/delete")
    @Operation(summary = "删除ivr-知识库问题")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:delete')")
    public CommonResult<Boolean> deleteIvrKnowledgeIssue(@RequestParam("id") Long id) {
        ivrKnowledgeIssueService.deleteIvrKnowledgeIssue(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ivr-知识库问题")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<IvrKnowledgeIssueRespVO> getIvrKnowledgeIssue(@RequestParam("id") Long id) {
        IvrKnowledgeIssueDO ivrKnowledgeIssue = ivrKnowledgeIssueService.getIvrKnowledgeIssue(id);
        return success(BeanUtils.toBean(ivrKnowledgeIssue, IvrKnowledgeIssueRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ivr-知识库问题分页")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<PageResult<IvrKnowledgeIssueRespVO>> getIvrKnowledgeIssuePage(@Valid IvrKnowledgeIssuePageReqVO pageReqVO) {
        PageResult<IvrKnowledgeIssueDO> pageResult = ivrKnowledgeIssueService.getIvrKnowledgeIssuePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IvrKnowledgeIssueRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ivr-知识库问题 Excel")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:export')")
    @OperateLog(type = EXPORT)
    public void exportIvrKnowledgeIssueExcel(@Valid IvrKnowledgeIssuePageReqVO pageReqVO,
                                             HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IvrKnowledgeIssueDO> list = ivrKnowledgeIssueService.getIvrKnowledgeIssuePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ivr-知识库问题.xls", "数据", IvrKnowledgeIssueRespVO.class,
                BeanUtils.toBean(list, IvrKnowledgeIssueRespVO.class));
    }


    @PostMapping("/default/create")
    @Operation(summary = "创建默认ivr-知识库问题-系统默认")
    @PreAuthorize("@ss.hasPermission('call:ivr-knowledge-issue-default:create')")
    public CommonResult<Long> createIvrKnowledgeIssueDefault(@Valid @RequestBody IvrKnowledgeIssueSaveReqVO createReqVO) {
        createReqVO.setFlowId(0L);
        createReqVO.setKnowledgeGroupId(0L);
        return success(TenantUtils.execute(0L, () -> ivrKnowledgeIssueService.createIvrKnowledgeIssue(createReqVO)));
    }

    @PutMapping("/default/update")
    @Operation(summary = "更新ivr-知识库问题-系统默认")
    @PreAuthorize("@ss.hasPermission('call:ivr-knowledge-issue-default:update')")
    public CommonResult<Boolean> updateIvrKnowledgeIssueDefault(@Valid @RequestBody IvrKnowledgeIssueSaveReqVO updateReqVO) {
        TenantUtils.execute(0L, () -> ivrKnowledgeIssueService.updateIvrKnowledgeIssue(updateReqVO));
        return success(true);
    }

    @DeleteMapping("/default/delete")
    @Operation(summary = "删除ivr-知识库问题-系统默认")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:ivr-knowledge-issue-default:delete')")
    public CommonResult<Boolean> deleteIvrKnowledgeIssueDefault(@RequestParam("id") Long id) {
        TenantUtils.execute(0L, () -> ivrKnowledgeIssueService.deleteIvrKnowledgeIssue(id));
        return success(true);
    }

    @GetMapping("/default/get")
    @Operation(summary = "获得ivr-知识库问题-系统默认")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:ivr-knowledge-issue-default:query')")
    public CommonResult<IvrKnowledgeIssueRespVO> getIvrKnowledgeIssueDefault(@RequestParam("id") Long id) {
        IvrKnowledgeIssueDO ivrKnowledgeIssue = TenantUtils.execute(0L, () -> ivrKnowledgeIssueService.getIvrKnowledgeIssue(id));
        return success(BeanUtils.toBean(ivrKnowledgeIssue, IvrKnowledgeIssueRespVO.class));
    }


    @GetMapping("/default/page")
    @Operation(summary = "获得ivr-知识库问题分页-系统默认")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<PageResult<IvrKnowledgeIssueRespVO>> getIvrKnowledgeIssuePageDefault(@Valid IvrKnowledgeIssuePageReqVO pageReqVO) {
        PageResult<IvrKnowledgeIssueDO> pageResult = TenantUtils.execute(0L, () -> ivrKnowledgeIssueService.getIvrKnowledgeIssuePage(pageReqVO));
        return success(BeanUtils.toBean(pageResult, IvrKnowledgeIssueRespVO.class));
    }


}